<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2006
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
* PREFERENCES class
*
* User preferences
*****/
class PREFERENCES
{
// Constructor
	function PREFERENCES($db)
	{
		$this->db = $db;
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/messages/SUCCESS.php");
		$this->success = new SUCCESS();
		include_once("core/messages/ERRORS.php");
		$this->errors = new ERRORS();
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
	}
// display options
	function display($error = FALSE)
	{
		include("config.php");
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
		$pString = FORM::formHeader('preferencesWrite');
		if($error)
		{
			$pString .= $error;
// As language may have changed, we create a new MESSAGES and SUCCESS object.
			$this->messages = new MESSAGES();
		}
		$this->template->setVar('heading', $this->messages->text("heading", "preferences"));
		$pString .= MISC::p(FORM::textInput($this->messages->text("config", "paging"), "paging", 
			$this->session->getVar("setup_paging"), 5) . " " . MISC::span('*', 'required') . MISC::br() . 
			$this->messages->text("hint", "pagingLimit"));
		$pString .= MISC::p(FORM::textInput($this->messages->text("config", "maxPaging"), "pagingMaxLinks", 
			$this->session->getVar("setup_pagingMaxLinks"), 5) . " " . MISC::span('*', 'required'));
		if(!$this->session->getVar("setup_pagingTagCloud"))
			$this->session->setVar("setup_pagingTagCloud", 100);
		$pString .= MISC::p(FORM::textInput($this->messages->text("config", "pagingTagCloud"), "pagingTagCloud", 
			$this->session->getVar("setup_pagingTagCloud"), 5) . " " . MISC::span('*', 'required') . 
			MISC::br() . $this->messages->text("hint", "pagingLimit"));
		$pString .= MISC::p(FORM::textInput($this->messages->text("config", "stringLimit"), "stringLimit", 
			$this->session->getVar("setup_stringLimit"), 5) . " " . MISC::span('*', 'required') . MISC::br() . 
			$this->messages->text("hint", "pagingLimit"));
		$input = $this->session->getVar("setup_pagingStyle") ? "A" : FALSE;
		$pString .= MISC::p(FORM::checkbox($this->messages->text("config", "pagingStyle"), 
			"pagingStyle", $input));
		$input = $this->session->getVar("setup_kwBibliography") ? "CHECKED" : FALSE;
		$pString .= MISC::p(FORM::checkbox($this->messages->text("config", "kwBibliography"), 
			"kwBibliography", $input));
		$input = $this->session->getVar("setup_useWikindxKey") ? "CHECKED" : FALSE;
		$pString .= MISC::p(FORM::checkbox($this->messages->text("config", "useWikindxKey"), 
			"useWikindxKey", $input));
		$input = $this->session->getVar("setup_useBibtexKey") ? "CHECKED" : FALSE;
		$pString .= MISC::p(FORM::checkbox($this->messages->text("config", "useBibtexKey"), 
			"useBibtexKey", $input));
		$input = $this->session->getVar("setup_displayBibtexLink") ? "CHECKED" : FALSE;
		$pString .= MISC::p(FORM::checkbox($this->messages->text("config", "displayBibtexLink"), 
			"displayBibtexLink", $input));
		$input = $this->session->getVar("setup_displayCmsLink") ? "CHECKED" : FALSE;
		$pString .= MISC::p(FORM::checkbox($this->messages->text("config", "displayCmsLink"), 
			"displayCmsLink", $input));
		$templates = TEMPLATE::loadDir();
		$pString .= MISC::p(FORM::selectedBoxValue($this->messages->text("config", "template"), 
				"template", $templates, $this->session->getVar("setup_template"), 4)
				 . " " . MISC::span('*', 'required'));
		include_once("core/messages/LOADLANGUAGE.php");
		$languages = LOADLANGUAGE::loadDir();
		$pString .= MISC::p(FORM::selectedBoxValue($this->messages->text("config", "language"), 
				"language", $languages, $this->session->getVar("setup_language")) 
				 . " " . MISC::span('*', 'required'));
		include_once("core/styles/LOADSTYLE.php");
		$styles = LOADSTYLE::loadDir("styles/bibliography");
		$pString .= MISC::p(FORM::selectedBoxValue($this->messages->text("config", "style") , "style", 
			$styles, $this->session->getVar("setup_style"), 4) . " " . MISC::span('*', 'required'));
		
		$pString .= MISC::p(FORM::formSubmit(), FALSE, "right");
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// write preferences
	function write($vars)
	{
// checkInput writes the session
		$this->checkInput($vars);
// If this is a logged on user, write preferences to WKX_user_preferences
		if($userId = $this->session->getVar("setup_userId"))
		{
			include_once("core/user/USER.php");
			$user = new USER($this->db);
			$user->writePreferences($userId);
		}
// As template may have changed, create new template object.
		$this->template = new TEMPLATE('content');
// As language may have changed, we create a new SUCCESS object.
		$success = NEW SUCCESS();
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $this->display($success->text("config")));
	}
// Check input
	function checkInput($vars)
	{
		include_once("core/html/MISC.php");
		include_once("core/init/SYSTEMCHECK.php");
// Store in session first and remove unrequired session variables
		include_once("core/session/SESSION.php");
		$session = new SESSION();
		$required = array("language", "template", "style", "paging", "pagingMaxLinks", "stringLimit",
			"pagingTagCloud");
		foreach($required as $key)
		{
			if(array_key_exists($key, $vars))
				$array[$key] = $vars[$key];
			else
				$session->delVar("preferences_" . $key);
		}
		if(isset($array))
			$session->writeArray($array, "preferences");
		if(array_key_exists("[pagingStyle", $vars))
			$session->setVar("preferences_pagingStyle", TRUE);
		else
			$session->delVar("preferences_pagingStyle");
		if(array_key_exists("kwBibliography", $vars))
			$session->setVar("preferences_kwBibliography", TRUE);
		else
			$session->delVar("preferences_kwBibliography");
		if(array_key_exists("useWikindxKey", $vars))
			$session->setVar("preferences_useWikindxKey", TRUE);
		else
			$session->delVar("preferences_useWikindxKey");
		if(array_key_exists("useBibtexKey", $vars))
			$session->setVar("preferences_useBibtexKey", TRUE);
		else
			$session->delVar("preferences_useBibtexKey");
		foreach($required as $value)
		{
			if(!array_key_exists($value, $vars) || !$vars[$value])
				$this->badInput($this->errors->text("inputError", "missing", " ($value) "));
		}
		if(!is_numeric($vars['paging']))
			$this->badInput($this->errors->text("inputError", "nan", " (paging) "));
		if(!is_numeric($vars['pagingTagCloud']))
			$this->badInput($this->errors->text("inputError", "nan", " (paging) "));
		if(!is_numeric($vars['pagingMaxLinks']))
			$this->badInput($this->errors->text("inputError", "nan", " (pagingMaxLinks) "));
		if(!is_numeric($vars['stringLimit']))
			$this->badInput($this->errors->text("inputError", "nan", " (stringLimit) "));
// All input good - write to session
		$session->writeArray($array, "setup");
		if(array_key_exists("pagingStyle", $vars))
			$session->setVar("setup_pagingStyle", 'A');
		else
			$session->delVar("setup_pagingStyle");
		if(array_key_exists("kwBibliography", $vars))
			$session->setVar("setup_kwBibliography", TRUE);
		else
			$session->delVar("setup_kwBibliography");
		if(array_key_exists("useWikindxKey", $vars))
			$session->setVar("setup_useWikindxKey", TRUE);
		else
			$session->delVar("setup_useWikindxKey");
		if(array_key_exists("useBibtexKey", $vars))
			$session->setVar("setup_useBibtexKey", TRUE);
		else
			$session->delVar("setup_useBibtexKey");
		if(array_key_exists("displayBibtexLink", $vars))
			$session->setVar("setup_displayBibtexLink", TRUE);
		else
			$session->delVar("setup_displayBibtexLink");
		if(array_key_exists("displayCmsLink", $vars))
			$session->setVar("setup_displayCmsLink", TRUE);
		else
			$session->delVar("setup_displayCmsLink");
	}
// Error handling
	function badInput($error)
	{
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $this->display($error));
	}
}
?>
